﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using V5_EaspLibs;

namespace V5_EaspLibs.Core {
    /// <summary>
    /// Dll接口加载类
    /// </summary>
    public class DataAccessHelper {
        private static readonly DataAccessHelper m_Instance = new DataAccessHelper();
        /// <summary>
        /// 
        /// </summary>
        public static DataAccessHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DataAccessHelper() {

        }
        /// <summary>
        /// 命名空间
        /// </summary>
        private readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        /// <summary>
        /// 创建缓存对象
        /// </summary>
        /// <param name="AssemblyPath"></param>
        /// <param name="ClassNamespace"></param>
        /// <returns></returns>
        public object CreateObject(string AssemblyPath, string ClassNamespace) {
            object objType = Easp.Cache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null) {
                try {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    Easp.Cache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch { }
            }
            return objType;
        }

        /// <summary>
        /// 创建数据层接口 是用隐式转换
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public object CreateIDAL(string className) {
            string ClassNamespace = AssemblyPath + "." + className;
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return objType;
        }
        /// <summary>
        /// 创建数据层接口 是用隐式转换
        /// </summary>
        /// <param name="NameSpace"></param>
        /// <param name="classNameSpace"></param>
        /// <returns></returns>
        public object CreateIDAL(string NameSpace, string classNameSpace) {
            object objType = CreateObject(NameSpace, classNameSpace);
            return objType;
        }
        /// <summary>
        /// 创建数据层接口 是用隐式转换e
        /// </summary>
        /// <param name="NameSpace"></param>
        /// <param name="pluginName"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public object CreateIDAL(string NameSpace, string pluginName, string className) {
            string AppNameSpace = NameSpace + "." + pluginName;
            string ClassNamespace = NameSpace + "." + pluginName + "." + className;
            object objType = CreateObject(AppNameSpace, ClassNamespace);
            return objType;
        }
    }
}
